package cn.edu.xmu.oomall.liquidation.dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.liquidation.mapper.LiquidationPoMapper;
import cn.edu.xmu.oomall.liquidation.model.bo.Expenditure;
import cn.edu.xmu.oomall.liquidation.model.bo.Liquidation;
import cn.edu.xmu.oomall.liquidation.model.po.LiquidationPo;
import cn.edu.xmu.oomall.liquidation.model.po.LiquidationPoExample;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author HertzLiu
 * @create_time 2021/12/5 17:29
 * @package_name cn.edu.xmu.oomall.liquidation.dao
 * @project_name oomall
 * @product_name IntelliJ IDEA
 * @description
 */

@Repository
public class LiquidationDao {
    @Autowired
    private LiquidationPoMapper liquidationPoMapper;

    public ReturnObject<PageInfo<LiquidationPo>> getLiquidatedListPageInfo(LiquidationPoExample liquidationPoExample, Integer page, Integer pageSize) {
        try {
            PageHelper.startPage(page, pageSize);
            List<LiquidationPo> liquidationPoList = liquidationPoMapper.selectByExample(liquidationPoExample);
            PageInfo<LiquidationPo> liquidationPoListPageInfo = PageInfo.of(liquidationPoList);
            return new ReturnObject<>(liquidationPoListPageInfo);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject<Object> getLiquidatedListDetail(Long shopId, Long id) {
        try {
            LiquidationPo liquidationPo = liquidationPoMapper.selectByPrimaryKey(id);
            if (liquidationPo == null) {
                return new ReturnObject<>(ReturnNo.RESOURCE_ID_NOTEXIST, "清算单id不存在");
            }
            if (!liquidationPo.getShopId().equals(shopId) && shopId != 0) {
                return new ReturnObject<>(ReturnNo.RESOURCE_ID_OUTSCOPE, "该清算单不属于本商铺");
            }
            return new ReturnObject<>(liquidationPo);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject<List<LiquidationPo>> getLiquidatedList(LiquidationPoExample liquidationPoExample) {
        try {
            List<LiquidationPo> liquidationPoList = liquidationPoMapper.selectByExample(liquidationPoExample);
            return new ReturnObject<>(liquidationPoList);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject insertLiquidationPo(LiquidationPo liquidationPo, Long adminId, String adminName) {
        try {
            Common.setPoCreatedFields(liquidationPo, adminId, adminName);
            int ret = liquidationPoMapper.insertSelective(liquidationPo);
            if (ret == 0) {
                return new ReturnObject<>(ReturnNo.FIELD_NOTVALID, "insertLiquidationPo报错");
            }
            return new ReturnObject<>(liquidationPo);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR, "insertLiquidationPo报错");
        }
    }

    public ReturnObject updateLiquidationPo(LiquidationPo liquidationPo) {
        try {
            int ret = liquidationPoMapper.updateByPrimaryKeySelective(liquidationPo);
            if (ret == 0) {
                return new ReturnObject<>(ReturnNo.FIELD_NOTVALID, "updateLiquidationPo报错");
            }
            return new ReturnObject<>(liquidationPo);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR, "updateLiquidationPo报错");
        }
    }

    public ReturnObject getShopNameByShopId(LiquidationPoExample liquidationPoExample) {
        try {
            String shopName = liquidationPoMapper.selectByExample(liquidationPoExample).get(0).getShopName();
            return new ReturnObject(shopName);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }
}